home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10407 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  6.3 KB

  1. Path: goanna.cs.rmit.EDU.AU!not-for-mail
  2. From: ok@goanna.cs.rmit.EDU.AU (Richard A. O'Keefe)
  3. Newsgroups: comp.lang.ada,comp.lang.c++,comp.lang.c,comp.lang.modula3,comp.lang.modula2
  4. Subject: Re: Hungarian notation - whoops!
  5. Date: 7 Mar 1996 19:20:53 +1100
  6. Organization: Comp Sci, RMIT, Melbourne, Australia
  7. Message-ID: <4hm695$na8@goanna.cs.rmit.EDU.AU>
  8. References: <30C40F77.53B5@swsbbs.com> <4h6hlo$hqu@goanna.cs.rmit.EDU.AU> <4h7vgdINNmsh@anvil.ugrad.cs.ubc.ca> <4hgd11$4p4@goanna.cs.rmit.EDU.AU> <4hhm9bINNqa3@keats.ugrad.cs.ubc.ca>
  9. NNTP-Posting-Host: goanna.cs.rmit.edu.au
  10. X-Newsreader: NN version 6.5.0 #0 (NOV)
  11.  
  12. c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku) writes:
  13. >So what are we to do? It is true that: some operations cannot be expressed in
  14. >the ``obvious way'' on two's complement arithmetic. However, it is also true
  15. >that some operations that are expressed in the ``obvious way'' under two's
  16. >complement _fail_ under a sign and magnitude machines!
  17.  
  18. Hokay, let's see what they are.  They can't be _arithmetic_ operations.
  19.  
  20. >The road goes both ways. Shall I give an example?
  21.  
  22. Fine.
  23.  
  24. >Under two's complement, I can convert a signed quantity into an unsigned
  25. >quantity such that the unsigned is _congruent_ to the signed, modulo a power of
  26. >two. I can do this just by chopping bits (if the unsigned quantity is as
  27. >narrow, or narrower than the signed type being converted). This is how standard
  28. >C defines the operation of converting a signed integral type into an unsigned
  29. >quantity that is narrower (K&R6.1, I think).
  30.  
  31. There are several questions here:
  32. (a) is this really a useful thing to do?
  33. (b) if it is, can the *operation* be implemented with a symmetric
  34.     representation of signed integers?
  35. (c) can it be implemented _just by masking_.
  36.  
  37. To which questions my answers are
  38. (a) not before the triumph of twos complement it wasn't useful
  39. (b) yes
  40. (c) no
  41.  
  42. Justification:
  43. (a) all I have to offer here is my personal experience.  I have been using
  44.     C on twos-complement machines since the beginning of 1980.  I used an
  45.     Algol dialect, Fortran, Pascal, and PL/I on a sign-and-magnitude machine 
  46.     for about 6 years before that.  In all that time, I have never wanted
  47.     to compute an unsigned number congruent to a signed number.  There are
  48.     lots of things I _have_ wanted, like fast gcd, but not that particular
  49.     operation.  It's one of those things you just don't think of when
  50.     signed integers have more bits than unsigned ones.  The absence of such
  51.     an operation never got in the way of any problem solving I had to do.
  52.  
  53.     Well, thanks to the triumph of 32-bit twos-complement machines, I can
  54.     think of one pretty important thing I would use this for: converting
  55.     integers to XDR representation.
  56.  
  57. (b) Let's take the B6700 as a concrete example, where the representation
  58.     was
  59.     word.[47:1]    unused for compatibility with B5500
  60.     word.[46:1]    significand sign
  61.     word.[45:1]    exponent sign
  62.     word.[39:6]    base-8 biassed exponent
  63.     word.[0:38]    non-negative integer
  64.     I hope I've got that right, it has been a long time.  Integers have
  65.     exponent 0.
  66.  
  67.     If I want a bit-string containing 40 bits that, if interpreted as an
  68.     unsigned integer, is congruent to a particular integer, then
  69.     DEFINE WORD = REAL;
  70.  
  71.     WORD PROCEDURE TO_UNSIGNED_40(X);
  72.     INTEGER X; VALUE X;
  73.     BEGIN
  74.         IF X >= 0 THEN BEGIN
  75.         TO_UNSIGNED_40 := X;
  76.         END ELSE BEGIN
  77.         WORD Y, LO, HI;
  78.  
  79.         Y := NOT(X);
  80.         LO := Y.[0:20] + 1;
  81.         HI := Y.[20:19] + LO.[20:1];
  82.         TO_UNSIGNED_40 := LO.[0:20] & HI[0:20:20];
  83.         END;
  84.     END;
  85.  
  86.     I've forgotten the exact syntax for field insertion; the intent of
  87.     the last assignment is
  88.     "take the bottom 20 bits of LO with zero fill,
  89.          and insert the bottom 20 bits of HI at offset 20".
  90.  
  91.     I have no B6700 to test this on, so I offer this only as an example
  92.     of the kind of code that could do the job.  If I want a smaller
  93.     number of bits, it's quite easy.  Suppose we know that X is in the
  94.     range -7 to +7 and we want to get a 4-bit unsigned version:
  95.  
  96.     WORD PROCEDURE TO_UNSIGNED_4(X);
  97.     INTEGER X; VALUE X;
  98.     BEGIN
  99.         IF ABS(X) > 7 THEN BEGIN
  100.         ERROR("TO_UNSIGNED_X: DOMAIN ERROR");
  101.         END ELSE
  102.         IF X < 0 THEN BEGIN
  103.         TO_UNSIGNED_4 := 16+X;
  104.         END ELSE BEGIN
  105.         TO_UNSIGNED_4 := X;
  106.         END;
  107.     END;
  108.  
  109.     The thing is, once you've got this bit string, what do you DO with it?
  110.  
  111. (c) Admittedly, it isn't a single instruction, and it isn't done by masking,
  112.     but "can get an unsigned version congruent mod a power of 2 by masking
  113.     alone" is a _definition_ of twos-complement.
  114.  
  115. The question is not whether you can do this, but whether it is a useful
  116. thing to do.  All I can honestly say is that in 
  117.  
  118. If we were concerned with strange bit twiddling of use in specialised
  119. areas, the DSP people with their bit-reversed addressing for use in FFTs
  120. would win.
  121.  
  122. >Don't tell me that two's complement doesn't have its neat and useful aspects!
  123. >It's a great idea that has been used successfully in many microprocessors!
  124.  
  125. Well, so were memory-to-memory instructions a great idea (for a hacker) that
  126. were used successfully in many machines, so was hardware support for
  127. decimal arithmetic (which I really don't want to knock), so were 8-bit
  128. characters, so were base-and-limit registers, so was microcode.  (The one
  129. time I had a potential use for WCS on a PDP-11/60 I couldn't find any
  130. explanation of how to use it, so I did without.)
  131.  
  132. Xerox had (because they _needed_) a 16-bit character set by 1984 (which is
  133. when I met it, the XNS character set).  The writing has been on the wall
  134. for 8-bit characters for some time, however neat and useful they may have
  135. been, and however successfully they may have been used.  There are even
  136. alternatives to floating point which deserve exploration.
  137.  
  138. In a world dominated by twos-complement machines, the practical utility of
  139. being one of the herd is beyond dispute.  When I went from the B6700 to a
  140. PDP-11, I felt _sick_.  I could appreciate how 2s complement made life
  141. easier for the architects, but it didn't help _me_, and "core dumped" was
  142. no substitute for the "Array subscript out of bounds at line #### called
  143. from ####" messages I was used to.  (The first C compiler of my acquaintance
  144. got some of its 'long' operations wrong, so I had to fix the compiler.  And
  145. 'long' was still what I was used to thinking of as 'short'.  Sigh.)
  146.  
  147. -- 
  148. The election is over, and Australia lost; the idjits elected _politicians_!
  149. Richard A. O'Keefe; http://www.cs.rmit.edu.au/~ok; RMIT Comp.Sci.
  150.